/**
 * Created by xuweijie on 2017/6/9.
 */

$(function () {
    $('#loginsub').click(function () {
        var username=$('#username').val();
        var passwd=$('#password').val();
        var type = validlogin(username,passwd);
        if(type!==false){
            if (type!=3) {
                var params = $.param({
                    username: username,
                    password: passwd,
                    type: type
                }, true);
                $.ajax({
                    url: '/resetpwd.action',
                    type: 'post',
                    data: params,
                    dateType: 'json',
                    success: function (data) {
                        if (data == "0") {
                            alert("密码重置成功，请登录");
                            window.location.href = "/front/frontlogin";
                        } else if (data == "1") {
                            alert("密码重置失败！请重试");
                            window.location.href = "/front/forgetpwd/showList";
                        } else if(data=="2"){
                            alert("新密码不可与旧密码相同");
                            window.location.href = "/front/forgetpwd/showList";
                        }else {
                            alert(data);
                            window.location.href = "/front/forgetpwd/showList";
                        }
                    }
                });
            }else {
                alert("Email/电话号码格式不正确，请重新输入");
            }
        }else {
            alert("请输入完整信息");
        }
    })
});

$(function () {
    $('#loginreset') .click(function () {
        $('#username').val("");
        $('#password').val("");
    })
});

function validlogin(username,passwd) {
    var flag = false;
    if(username==="" || passwd===""){
        flag = false;
    }else if(username.indexOf('@')!==-1){
        flag = 1;
    }else if( /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(username)){
        flag = 2;
    }else{
        flag = 3;
    }
    return flag;
}